package Foundation;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import TechnicalServices.DataConnector;
import TechnicalServices.IMapper;
import DomainPackage.*;
public class KargoUcusMapper implements IMapper {

	@Override
	public Object get(int ID) {
		KargoUcus kargoucus = new KargoUcus();
		ResultSet rs = DataConnector.getInstance().query("select * from kargoucus where ucusid = " + ID + "");
		int int3=0;
		int int6=0;
		int int7=0;
		if (rs != null){
            
            try {
            		rs.next();
                    int3 = Integer.parseInt(rs.getString(3));
                    int6 = Integer.parseInt(rs.getString(6));
                    int7 = Integer.parseInt(rs.getString(7));
                    kargoucus.setUcusID(Integer.parseInt(rs.getString(1)));
            		kargoucus.setUcusTarihi(new GregorianCalendar(Integer.parseInt(rs.getString(2).split("-")[0]), 
							Integer.parseInt(rs.getString(2).split("-")[1]), 
							Integer.parseInt(rs.getString(2).split("-")[2])));

            		kargoucus.setHacim(Float.valueOf(rs.getString(4)));
            		kargoucus.setFiyat(Float.valueOf(rs.getString(5)));
            		
                   
            } catch (NumberFormatException e) {
                    System.out.println("KargoUcusMapper: number format problem");
                    e.printStackTrace();
            } catch (SQLException e) {
                    System.out.println("KargoUcusMapper: sql problem");
                    e.printStackTrace();
            }
		}
		else
            System.out.println("no kargoucus with given ID");
		PilotMapper pm = new PilotMapper();
		KargoUcakMapper kum = new KargoUcakMapper();
		SeferMapper sm = new SeferMapper();
		
        Pilot p = (Pilot) pm.get(int6);
        Sefer s = (Sefer) sm.get(int7);
		KargoUcak ku = (KargoUcak) kum.get(int3);
		kargoucus.setPilot(p);
		kargoucus.setSefer(s);
		kargoucus.setUcak(ku);
		return kargoucus;
	}

	@Override
	public int put(Object object) {
		KargoUcus kargoucus=(KargoUcus) object;
		String dt = "";
		GregorianCalendar dta = kargoucus.getUcusTarihi();
		dt += String.valueOf(dta.get(GregorianCalendar.YEAR)) + "-";
		dt += String.valueOf(dta.get(GregorianCalendar.MONTH)) + '-';
		dt += String.valueOf(dta.get(GregorianCalendar.DAY_OF_MONTH));
		if(!DataConnector.getInstance().update("INSERT INTO kargoucus "+
				"(ucusid, ucustarihi, ucak, hacim, fiyat, pilot, sefer) " +
				"values ('"+kargoucus.getUcusID()+"', '"+
				dt+"', '"+
				kargoucus.getUcak().getUcakID()+"', '"+
				kargoucus.getHacim()+"', '"+
				kargoucus.getFiyat()+"', '"+
				kargoucus.getPilot().getPilotID()+"', '"+
				kargoucus.getSefer().getSeferID()+
				"')")){
			System.out.println("error");
		}
		return 0;
	}

	@Override
	public List<Object> getAll() {
		List<Object> allKargoUcusList = new ArrayList<Object>();
    	ResultSet rs = DataConnector.getInstance().query("select * from kargoucus;");
    	int numberOfRows=0;
		try {
			ResultSetMetaData rsMetaData = rs.getMetaData();
            numberOfRows = rsMetaData.getColumnCount()+1;
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

    try {
    	if (rs != null){
            while(rs.next()){
                    
                    KargoUcus kargoucus= new KargoUcus(-1); 
                    	KargoUcak u = new KargoUcak();
                    	u.setUcakID(Integer.parseInt(rs.getString(3)));
                    	Pilot p = new Pilot(Integer.parseInt(rs.getString(6)));
                    	Sefer s = new Sefer();
                    	s.setSeferID(Integer.parseInt(rs.getString(7)));                    	
                    	kargoucus.setUcusID(Integer.parseInt(rs.getString(1)));
                		kargoucus.setUcusTarihi(new GregorianCalendar(Integer.parseInt(rs.getString(2).split("-")[0]), 
    							Integer.parseInt(rs.getString(2).split("-")[1]), 
    							Integer.parseInt(rs.getString(2).split("-")[2])));
                		kargoucus.setUcak(u);
                		kargoucus.setHacim(Float.valueOf(rs.getString(4)));
                		kargoucus.setFiyat(Float.valueOf(rs.getString(5)));
                		kargoucus.setPilot(p);
                		kargoucus.setSefer(s);
                            allKargoUcusList.add(kargoucus);             
            }
    	}
    }
    catch(Exception e){
    	e.printStackTrace();
    }
    	for (Object se: allKargoUcusList){
    		KargoUcus ku = (KargoUcus) se;
    		KargoUcakMapper kum = new KargoUcakMapper();
    		PilotMapper pm = new PilotMapper();
    		SeferMapper sm = new SeferMapper();
    		KargoUcak k = (KargoUcak) kum.get(ku.getUcak().getUcakID());
    		Pilot p = (Pilot) pm.get(ku.getPilot().getPilotID());
    		Sefer s = (Sefer) sm.get(ku.getSefer().getSeferID());
    		
    		ku.setPilot(p);
    		ku.setSefer(s);
    		ku.setUcak(k);
    	}
    	return allKargoUcusList;		
	
	}

}
